﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;

using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.datatype;
using umbraco.interfaces;
using uTube.Core.Extensions;

namespace uTube.Core.DataTypes.YouTubeUploader
{
	public class YTU_DataType : AbstractDataEditor
	{
		/// <summary>
		/// The render control.
		/// </summary>
		private YTU_Control m_Control = new YTU_Control();

		/// <summary>
		/// The Data object for the data-type.
		/// </summary>
		private IData m_Data;

		/// <summary>
		/// The PreValue Editor for the data-type.
		/// </summary>
		private YTU_PrevalueEditor m_PreValueEditor;

		/// <summary>
		/// Initializes a new instance of the <see cref="YTU_DataType"/> class.
		/// </summary>
		public YTU_DataType()
			: base()
		{
			// set the render control as the placeholder
			this.RenderControl = this.m_Control;

			// assign the initialise event for the placeholder
			this.m_Control.Init += new EventHandler(this.m_Control_Init);

			// assign the save event for the data-type/editor
			this.DataEditorControl.OnSave += new AbstractDataEditorControl.SaveEventHandler(this.DataEditorControl_OnSave);
		}

		/// <summary>
		/// Gets the data.
		/// </summary>
		/// <value>The data.</value>
		public override IData Data
		{
			get
			{
				if (this.m_Data == null)
				{
					this.m_Data = new YTU_Data(this);
				}

				return this.m_Data;
			}
		}

		/// <summary>
		/// Gets the name of the data type.
		/// </summary>
		/// <value>The name of the data type.</value>
		public override string DataTypeName
		{
			get
			{
				return "uTube: YouTube Video Uploader";
			}
		}

		/// <summary>
		/// Gets the id of the data-type.
		/// </summary>
		/// <value>The id of the data-type.</value>
		public override Guid Id
		{
			get
			{
				return new Guid("31235fd3-d366-49c1-ab19-9d3ec5d9bec0");
			}
		}

		/// <summary>
		/// Gets the prevalue editor.
		/// </summary>
		/// <value>The prevalue editor.</value>
		public override IDataPrevalue PrevalueEditor
		{
			get
			{
				if (this.m_PreValueEditor == null)
				{
					this.m_PreValueEditor = new YTU_PrevalueEditor(this);
				}

				return this.m_PreValueEditor;
			}
		}

		/// <summary>
		/// Handles the Init event of the m_Placeholder control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void m_Control_Init(object sender, EventArgs e)
		{
			// get the render control options from the Prevalue Editor.
			var options = ((YTU_PrevalueEditor)this.PrevalueEditor).GetPreValueOptions<YTU_Options>();

			if (this.Data.Value != null)
			{
				this.m_Control.VideoId = this.Data.Value.ToString();
			}

			// set the render control options
			this.m_Control.Options = options;
		}

		/// <summary>
		/// Saves the editor control value.
		/// </summary>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void DataEditorControl_OnSave(EventArgs e)
		{
			string videoId = string.Empty;
			var postedFile = this.m_Control.PostedFile;

			string videoTitle = string.Empty;
			string videoDescription = string.Empty;
			string keywords = string.Empty;
			string category = string.Empty;
			bool privateVideo = false;

			NameValueCollection form = HttpContext.Current.Request.Form;
			if (form.AllKeys.Any())
			{
				foreach (string s in form.AllKeys.Where(s => s.Contains("uTubeVideoTitle")))
				{
					videoTitle = form.Get(s);
				}

				foreach (string s in form.AllKeys.Where(s => s.Contains("uTubeVideoDescription")))
				{
					videoDescription = form.Get(s);
				}

				foreach (string s in form.AllKeys.Where(s => s.Contains("uTubeKeywords")))
				{
					keywords = form.Get(s);
				}

				foreach (string s in form.AllKeys.Where(s => s.Contains("uTubeVideoCategory")))
				{
					category = form.Get(s);
				}

				foreach (string s in form.AllKeys.Where(s => s.Contains("uTubePrivateVideo")))
				{
					privateVideo = s.Equals("on");
				}
			}

			if (postedFile != null && !string.IsNullOrEmpty(videoTitle) && !string.IsNullOrEmpty(videoDescription) && !string.IsNullOrEmpty(keywords))
			{
				VideoUploader videoUploader = new VideoUploader(m_Control.Options.ApplicationName,
																m_Control.Options.DeveloperKey, postedFile,
																videoTitle, videoDescription, keywords, category, privateVideo);

				videoId = string.IsNullOrEmpty(m_Control.Options.AuthToken) ?
					videoUploader.ClientLoginVideoEntry(m_Control.Options.Username, m_Control.Options.Password) :
					videoUploader.OAuthVideoEntry(m_Control.Options.AuthToken);
			}

			// TODO: add update video when properties change and Video Id exists

			// save the value of the control depending on whether a new file is uploaded
			this.Data.Value = string.IsNullOrEmpty(videoId) ? this.m_Control.VideoId : videoId;
		}
	}
}